package org.ourgrid.peer.business.requester;
import java.util.ArrayList;
import java.util.List;

import org.ourgrid.common.internal.IResponseTO;
import org.ourgrid.common.internal.response.LoggerResponseTO;
import org.ourgrid.common.internal.response.OperationSucceedResponseTO;
import org.ourgrid.peer.business.controller.messages.PeerControlMessages;
import org.ourgrid.peer.request.AddWorkerRequestTO;

public class AddWorkerRequester extends AbstractRegisterWorkerRequester<AddWorkerRequestTO> {

	public List<IResponseTO> execute(AddWorkerRequestTO request) {
		
		List<IResponseTO> responses = new ArrayList<IResponseTO>();
		
		if(request.canComponentBeUsed()) {
			
			if(!request.isThisMyPublicKey()) {
				LoggerResponseTO loggerTO = new LoggerResponseTO(PeerControlMessages.getUnknownSenderSettingWorkersMessage(
						request.getSenderPubKey()), LoggerResponseTO.WARN  );
				responses.add(loggerTO);
				
				return responses;
			}
			
			System.out.println("addWorkerRequester");
			
			registerNewWorker(responses, request.getWorkerSpec(), 
					request.getMyUserAtServer());
			OperationSucceedResponseTO to = new OperationSucceedResponseTO();
			to.setClientAddress(request.getClientAddress());
		}
		
		return responses;
		
	}
}
